﻿using System;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Practices.Prism.Commands;
using StepsManagerLibrary.Enum;

namespace WPF_Example.ViewModel
{
    /// <summary>
    /// ViewModel to be used on an observer
    /// </summary>
    public class ObserverViewModel : BaseViewModel, StepsManagerLibrary.Interfaces.IObserver<StepControlStateEnum>
    {
        #region Fields
        
        /// <summary>
        /// IStepControl from which it receives notifications
        /// </summary>
        private readonly string _controlTitle; 
        
        #endregion

        #region Commands

        #region MoveHere
        
        // Move to the step hosting the Observer
        private readonly ICommand _moveHereCommand;
        public ICommand MoveHereCommand
        {
            get { return _moveHereCommand; }
        }

        // TODO : Update the Observer class so that used with
        // TODO : IStepControl it can move to the expected step

        private void MoveHereAction()
        {
            // Notify the mother class
        }

        #endregion

        #endregion

        #region Properties
        // Title of the step
        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                OnPropertyChanged("Title");
            }
        }

        // Color of the state
        private SolidColorBrush _stepStateColor;
        public SolidColorBrush StepStateColor
        {
            get { return _stepStateColor; }
            set
            {
                _stepStateColor = value;
                OnPropertyChanged("StepStateColor");
            }
        } 
        #endregion

        #region Constructors
        
        /// <summary>
        /// Creates an ObserverViewModel
        /// </summary>
        /// <param name="title">
        /// Adding some information to the observer
        /// </param>
        public ObserverViewModel(string title)
        {
            // Creating the commands
            _moveHereCommand = new DelegateCommand(MoveHereAction);

            // Setting up the properties
            _controlTitle = title;
            
            Title = String.Format("{0} has not yet been visited", _controlTitle);
            StepStateColor = new SolidColorBrush(Colors.DarkBlue);
        } 

        #endregion

        #region IObserver<StepControlStateEnum> Members

        /// <summary>
        /// The observer is being notified that the state of the StepControl it is 
        /// attached with has been updated
        /// </summary>
        /// <param name="notification"></param>
        public void Notify(StepControlStateEnum notification)
        {
            // Updating the UI elements based on the information we receive

            if (notification == StepControlStateEnum.Selected)
            {
                Title = String.Format("{0} has been selected", _controlTitle);
                StepStateColor = new SolidColorBrush(Colors.GreenYellow);
            }

            if (notification == StepControlStateEnum.NotSelected)
            {
                Title = String.Format("{0} has been unselected", _controlTitle);
                StepStateColor = new SolidColorBrush(Colors.DarkBlue);
            }

            if (notification == StepControlStateEnum.MovingTo || notification == StepControlStateEnum.MovingFrom)
            {
                Title = String.Format("{0} is currently in transition", _controlTitle);
                StepStateColor = new SolidColorBrush(Colors.Orange);
            }
        } 

        #endregion
    }
}
